package com.tobby.camel.camelinaction.chapter2.recipientlist;

import javax.jms.ConnectionFactory;

import org.apache.activemq.ActiveMQConnectionFactory;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.component.jms.JmsComponent;
import org.apache.camel.impl.DefaultCamelContext;

public class OrderRouterWithRecipientListBean {

	public static void main(String[] args) throws Exception {
		CamelContext context = new DefaultCamelContext();
		ConnectionFactory connectionFactory = new ActiveMQConnectionFactory(
				"vm://localhost");
		context.addComponent("jms",
				JmsComponent.jmsComponentAutoAcknowledge(connectionFactory));
		context.addRoutes(new RouteBuilder() {

			@Override
			public void configure() throws Exception {
				from("file:src/main/resources/data?noop=true").to("jms:incomingOrders");
				from("jms:incomingOrders").choice()
						.when(header("CamelFileName").endsWith(".xml"))
						.to("jms:xmlOrder");
				from("jms:xmlOrder").bean(RecipientListBean.class);

				from("jms:accounting").process(new Processor() {

					@Override
					public void process(Exchange exchange) throws Exception {
						System.out.println("Accounting Received order : "
								+ exchange.getIn().getHeader("CamelFileName"));
					}
				});
				from("jms:production").process(new Processor() {

					@Override
					public void process(Exchange exchange) throws Exception {
						System.out.println("Production received order : "
								+ exchange.getIn().getHeader("CamelFileName"));
					}
				});
			}
		});
		context.start();
		Thread.sleep(10000);
		context.stop();
	}

}
